package com.iot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iot.core.JsonHelper;
import com.iot.core.LResult;
import com.iot.core.PageData;
import com.iot.pojo.MTwork_zd;
import com.iot.service.Twork_zdSerivce;
import com.iot.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/workZd/")
public class TWork_zdController  extends BaseController  {
    @Autowired
    private Twork_zdSerivce service;
    @ResponseBody
    @RequestMapping("save")
    public String save(@RequestParam String bean) {
        try {

            MTwork_zd entity = JsonHelper.toBean(bean, MTwork_zd.class);
            if (entity.getWORK_ZD_ID() > 0) {
                service.update(entity);
            } else {
                service.insert(entity);

            }
            return LResult.Success(entity.getWORK_ZD_ID()).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }
    @ResponseBody
    @RequestMapping("get")
    public String getByid(@RequestParam Integer id) {
        try {
            //  String menu = request.getParameter("menu");
            MTwork_zd bean = service.single(id);
            return LResult.Success(bean).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("del")
    public String deleteByid(@RequestParam Integer id) {
        try {
            //  String menu = request.getParameter("menu");
            service.deleteById(id);
            return LResult.Success().toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }
    @ResponseBody
    @RequestMapping("query")
    public String queryData() {

        try {
            PageData map = getPageData();
            if (!map.containsKey("oworkNo")) {
                map.put("oworkNo", "");
            }
            if (!map.containsKey("nworkNo")) {
                map.put("nworkNo", "");
            }
            if (!map.containsKey("sdate")) {
                map.put("sdate", "");
            }
            if (!map.containsKey("edate")) {
                map.put("edate", "");
            } else {
                String strSdate = map.get("edate").toString();
               if(strSdate!="") {
                   String tempdate = Common.getShortDate(Common.getSomeDay(strSdate, 1));
                   map.put("edate", tempdate);
               }
            }


            PageHelper.startPage(Common.toInt(map.get("page")), Common.toInt(map.get("limit")));
            List<Map<String, Object>> list = service.fetchByPage(map);
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
            return LResult.Success(pageInfo.getTotal(), pageInfo.getList()).toJson();
        } catch (Exception ex) {
            return  LResult.Error("数据错误").toJson();
        }
    }


}
